package com.opensky.module.sys.domain.jpa.entity;


import com.opensky.module.sys.domain.jpa.repository.SysUserRepository;
import com.opensky.module.sys.interfaces.frm.SysUserQueryFrm;
import com.opensky.common.infrastructure.base.jpa.BaseEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.criteria.Predicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * 用户表(表sys_user)ORM实体类
 *
 * @author 中国印
 * @date 2021-11-26 14:24:35
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicUpdate
@DynamicInsert
@Table(name = "sys_user")
public class SysUser extends BaseEntity<SysUserRepository, SysUser, Long> {

    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    @Column(name = "id", insertable = false, updatable = false, nullable = false, columnDefinition = "bigint(20) COMMENT '表主键'")
    private Long id;
    /**
     * 登录名
     */
    @Column(name = "login_name")
    private String loginName;
    /**
     * 登陆密码
     */
    @Column(name = "login_passwd")
    private String loginPasswd;
    /**
     * 姓名
     */
    @Column(name = "realname")
    private String realname;
    /**
     * 性别。0：未知；1：男；2：女
     */
    @Column(name = "sex")
    private Integer sex;
    /**
     * 手机号码
     */
    @Column(name = "phone")
    private String phone;
    /**
     * 固定电话
     */
    @Column(name = "tel")
    private String tel;
    /**
     * 邮箱
     */
    @Column(name = "email")
    private String email;
    /**
     * 用户头像
     */
    @Column(name = "avatar")
    private String avatar;
    /**
     * 职务名称
     */
    @Column(name = "job_title")
    private String jobTitle;
    /**
     * 用户状态。0：正常；1：禁用
     */
    @Column(name = "status")
    private Integer status;
    /**
     * 排序。值越小越靠前
     */
    @Column(name = "sort")
    private Integer sort;
    /**
     * 创建人
     */
    @Column(name = "create_by")
    private Long createBy;
    /**
     * 修改人
     */
    @Column(name = "update_by")
    private Long updateBy;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    private static SysUserRepository repository() {
        return SysUser.Holder.REPO;
    }

    private static final class Holder {
        private static final SysUserRepository REPO = getRepository(SysUserRepository.class, SysUser.class);
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<SysUser> getSpecification(SysUserQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (frm.getId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (!StringUtils.isEmpty(frm.getLoginName())) {
                predicateList.add(criteriaBuilder.equal(root.get("loginName"), frm.getLoginName()));
            }
            if (!StringUtils.isEmpty(frm.getLoginPasswd())) {
                predicateList.add(criteriaBuilder.equal(root.get("loginPasswd"), frm.getLoginPasswd()));
            }
            if (!StringUtils.isEmpty(frm.getRealname())) {
                predicateList.add(criteriaBuilder.equal(root.get("realname"), frm.getRealname()));
            }
            if (frm.getSex() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("sex"), frm.getSex()));
            }
            if (!StringUtils.isEmpty(frm.getPhone())) {
                predicateList.add(criteriaBuilder.equal(root.get("phone"), frm.getPhone()));
            }
            if (!StringUtils.isEmpty(frm.getTel())) {
                predicateList.add(criteriaBuilder.equal(root.get("tel"), frm.getTel()));
            }
            if (!StringUtils.isEmpty(frm.getEmail())) {
                predicateList.add(criteriaBuilder.equal(root.get("email"), frm.getEmail()));
            }
            if (!StringUtils.isEmpty(frm.getAvatar())) {
                predicateList.add(criteriaBuilder.equal(root.get("avatar"), frm.getAvatar()));
            }
            if (!StringUtils.isEmpty(frm.getJobTitle())) {
                predicateList.add(criteriaBuilder.equal(root.get("jobTitle"), frm.getJobTitle()));
            }
            if (frm.getStatus() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("status"), frm.getStatus()));
            }
            if (frm.getSort() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("sort"), frm.getSort()));
            }
            if (frm.getCreateBy() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("createBy"), frm.getCreateBy()));
            }
            if (frm.getUpdateBy() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("updateBy"), frm.getUpdateBy()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

}
